package com.example.service.impl;

import com.example.mapper.measuringToolChecklistMapp;
import com.example.pojo.Result;
import com.example.pojo.measuringToolChecklistPojo;
import com.example.service.measuringToolChecklistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class measuringToolChecklistImpl implements measuringToolChecklistService {
    @Autowired
    private measuringToolChecklistMapp measuringToolChecklistMapp;
    public List<measuringToolChecklistPojo> queryOneField(Map<String,Object> map){
       String fieldName = (String) map.get("fieldName");
       String maxValStr= (String) map.get("maxVal");
       String minValStr= (String) map.get("minVal");
       DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
       LocalDateTime maxVal = LocalDateTime.parse(maxValStr, formatter);
       LocalDateTime minVal = LocalDateTime.parse(minValStr, formatter);
       List<measuringToolChecklistPojo> list = measuringToolChecklistMapp.queryOneField(fieldName,minVal,maxVal);
       return list;
   }
   //编辑某一行
    public void editOneById(measuringToolChecklistPojo measuringToolChecklistPojo) {
        //定义修改时间生成规则
        measuringToolChecklistPojo.setUpdate_time(LocalDateTime.now());
        //调用 Mapper 编辑数据
        measuringToolChecklistMapp.editOneById(measuringToolChecklistPojo);
    }

    //对一个字段，从老值更改为新值
    public void editByField(String fieldName, String old, String newData){
        measuringToolChecklistMapp.editByField(fieldName,old,newData);
    }

    //查询点检时间及编号
    public List<measuringToolChecklistPojo> queryTimeAndNumber(LocalDateTime startTime, LocalDateTime endTime, String numberUse){
        List<measuringToolChecklistPojo> list = measuringToolChecklistMapp.queryTimeAndNumber(startTime, endTime, numberUse);
        return list;
    }

    //第一步：向点检记录中插入一行
//    public void insertOne(measuringToolChecklistPojo pojo){
//        //生成一段低代码格式的id
//        UUID uuid = UUID.randomUUID();
//        String uuidStr = uuid.toString().replace("-", "");
//        pojo.setId(uuidStr);
//        //定义创建时间生成规则
//        if (pojo.getCreate_time().equals("")||pojo.getCreate_time()==null) {
//            pojo.setCreate_time(LocalDateTime.now());
//        }
//        //定义修改时间生成规则
//        pojo.setUpdate_time(LocalDateTime.now());
//        measuringToolChecklistMapp.insertOne(pojo);
//    }
    public void insertOne(measuringToolChecklistPojo pojo){
        // 参数校验
        if (pojo == null) {
            throw new IllegalArgumentException("pojo cannot be null");
        }

        // 生成一段低代码格式的id
        UUID uuid = UUID.randomUUID();
        String uuidStr = uuid.toString().replace("-", "");
        pojo.setId(uuidStr);

        // 修复：先检查是否为null，再调用equals方法
        if (pojo.getCreate_time() == null || pojo.getCreate_time().equals("")) {
            pojo.setCreate_time(LocalDateTime.now());
        }

        // 定义修改时间生成规则
        pojo.setUpdate_time(LocalDateTime.now());
        measuringToolChecklistMapp.insertOne(pojo);
    }

    //查询点检记录表中编号及产生时间为目标值的数据
    public List<measuringToolChecklistPojo> queryTimeNumber(Map<String, Object> map){
        String number = (String) map.get("number");
        String date = (String) map.get("date");
        LocalDateTime dateTime = LocalDateTime.parse(date, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        List <measuringToolChecklistPojo> pojoList=measuringToolChecklistMapp.queryTimeNumber(number, dateTime);
        return pojoList;
    }

    //把当日点检的所有记录调出来
    public List<measuringToolChecklistPojo> queryBetweenTwoTime(String fieldName, LocalDateTime startTime, LocalDateTime endTime){
        List<measuringToolChecklistPojo> pojoList=measuringToolChecklistMapp.queryBetweenTwoTime(fieldName,startTime,endTime);
        return pojoList;
    }
}
